package com.aoxin.service.impl.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aoxin.mapper.opm.SlfUserMapper;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aoxin.mapper.course.webCourse.MyCoursePageMapper;
import com.aoxin.mapper.opm.SlfEmployeeMapper;
import com.aoxin.model.dto.SenSmsDto;
import com.aoxin.model.dto.UserDto;
import com.aoxin.service.inter.system.CommonService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.RandomNumberUtil;
import com.aoxin.util.RedisUtil;
import com.aoxin.util.SendSmsUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("commonService")
public class CommonServiceImpl implements CommonService {

	private Log log = LogFactory.getLog(CommonServiceImpl.class);
	@Autowired
	private SlfUserMapper slfUserMapper;
	@Autowired
	private SlfEmployeeMapper slfEmployeeMapper;
	
	@Autowired
	private MyCoursePageMapper myCoursePageMapper;

	/**
	 * 发送验证码 判断手机号是否注册
	 * @param phone
	 *            手机号
	 */
	@Override
	public void sendIdentifyCode(String phone,String flag) {
		// 判断手机号是否已注册
        Long isRegister = this.slfUserMapper.checkPhone(phone, null);
        if(flag.equalsIgnoreCase("0")){
            //已经注册
            if(isRegister>0){
                throw new IllegalArgumentException(String.format(ErrorUtil.HAS_PHONE,phone));
            }
        }else if(flag.equalsIgnoreCase("1")){
            //登录
            if(isRegister==0){
                throw new IllegalArgumentException(ErrorUtil.NO_PHONE);
            }
        }
		String IdentifyCode = RandomNumberUtil.getRandomNumber();
		log.info(String.format(ErrorUtil.LOGIN_IDENTIFY_CODE, IdentifyCode));
		String oldIdentifyCode = (String) RedisUtil.get(phone + CommonService.PHONE_IDENTIFY_CODE);
		if (null != oldIdentifyCode) {
			RedisUtil.del(phone + CommonService.PHONE_IDENTIFY_CODE);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", IdentifyCode);
		String message = JSONObject.fromObject(map).toString();
		log.info(message);
		SendSmsUtil.SendSms(phone, message, SenSmsDto.REGISTER);
		RedisUtil.set(phone + CommonService.PHONE_IDENTIFY_CODE, IdentifyCode);
	}

	@Override
	public List<UserDto> getUserDtoList(Map<String, Object> map) {

		return this.slfEmployeeMapper.getUserDtoList(map);
	}

	/**
	 * 读取取消预约退钱金额
	 * @param seconds
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Integer getCancelMoney(String seconds,Integer id) {
		 Map<String,Object> map = new HashMap<String, Object>();
		 Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
		 map.put("id",id);
		 map.put("childrenId",childrenId);
		 //找到预约价格
		 double money = myCoursePageMapper.getBespokePrice(map);
		 String day = seconds.substring(0,2);
	     String hours = seconds.substring(3,5);
	     String min = seconds.substring(7,9);
	     String second = seconds.substring(seconds.length()-3,seconds.length()-1);
	     double daySeconds = Integer.parseInt(day)*86400;
	     double hoursSeconds = Integer.parseInt(hours)*3600;
	     double minSeconds = Integer.parseInt(min)*60;
	     double newSeconds = daySeconds+hoursSeconds+minSeconds+Integer.parseInt(second);
	     double hour = newSeconds / 3600;
		 if(hour < 3){
		 double m = (newSeconds % 3600)/60;
		 double moneyDouble = m/60*(double)money;
		 moneyDouble = (double)Math.round(moneyDouble * 100) / 100; 
		 return (int)moneyDouble;
		 }else{
		 return (int)money;
		 }
	}
	
}
